package cn.wx.scholar.tool.generator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

import com.baomidou.mybatisplus.generator.config.po.TableField;

/**
 * @ClassName: CreateHtmlUtils  
 * @Description: Java 根据模板创建 html
 * @author 
 * @date 2016年4月22日 下午3:51:16
 */
public class CreateHtml {
    
	
	private static File file = new File("");
    private static String path = file.getAbsolutePath();
    private static String filePath = path + "\\src\\main\\resources\\templates\\template.html.vm";
    private static String disrPath = path + "\\src\\main\\resources\\static\\view";
    
    public static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }
    
    /**
     * @Title: MakeHtml 
     * @Description: 创建html
     * @param    filePath 设定模板文件
     * @param    imagePath 需要显示图片的路径
     * @param    disrPath  生成html的存放路径
     * @param    fileName  生成html名字 
     * @return void    返回类型 
     * @throws
     */
    public static void MakeHtml(String fileName,List<TableField> list){
        try {
        	
        	StringBuffer dowrite4 = new StringBuffer();
        	StringBuffer dowrite3 = new StringBuffer();
        	StringBuffer dowrite2 = new StringBuffer();
        	StringBuffer dowrite5 = new StringBuffer();
        	for(TableField tfd : list){
        		
        		if("fRemove".equals(tfd.getPropertyName())){
        			continue;
        		}
        		
        		dowrite3.append("<td>{{item." + tfd.getPropertyName() + "}}</td>\r\n");
        		dowrite4.append("<th>"+ tfd.getComment() +"</th>\r\n");
        		
        		
        		if(!"fAddTime".equals(tfd.getPropertyName())){
            		dowrite2 
            		.append("<div class=\"input-group m-b\">\r\n")
            		.append("   <span class=\"input-group-addon\">"+tfd.getComment()+"</span>\r\n");
            		
            		if(tfd.getPropertyType().equals("Date")){
            			
            			dowrite5.append("<script src=\"../js/laydate.js\"></script>");
            			
            			dowrite2.append("   <input onclick=\"laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})\" type=\"text\" class=\"form-control laydate-icon\" v-model=\"item."+ tfd.getPropertyName() +"\">\r\n");
            		}else{
            			dowrite2.append("   <input type=\"text\" class=\"form-control\" v-model=\"item."+ tfd.getPropertyName() +"\">\r\n");
            		}
            		
            		dowrite2.append("</div>\r\n");
        		}

        	}
        	
        	
            System.out.print(filePath);
            String templateContent = "";
            FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件
            int lenght = fileinputstream.available();
            byte bytes[] = new byte[lenght];
            fileinputstream.read(bytes);
            fileinputstream.close();
            templateContent = new String(bytes);
            templateContent = templateContent.replaceAll("#####dowrite3#####", dowrite3.toString());
            templateContent = templateContent.replaceAll("#####dowrite4#####", dowrite4.toString());
            templateContent = templateContent.replaceAll("#####dowrite2#####", dowrite2.toString());
            templateContent = templateContent.replaceAll("#####dowrite1#####", toLowerCaseFirstOne(fileName));
            templateContent = templateContent.replaceAll("#####dowrite5#####", dowrite5.toString());
            String fileame = fileName.toLowerCase() + ".html";
            String fileame_ = disrPath+"/" + fileame;// 生成的html文件保存路径。
            FileOutputStream fileoutputstream = new FileOutputStream(fileame_);// 建立文件输出流
            System.out.print("文件输出路径:");
            System.out.print(fileame);
            byte tag_bytes[] = templateContent.getBytes();
            fileoutputstream.write(tag_bytes);
            fileoutputstream.close();
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
    
    
}